package com.lzx.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.common.utils.R;
import com.lzx.user.entity.Inform;
import com.lzx.user.entity.vo.NicknameVO;
import com.lzx.user.entity.vo.OssVO;
import com.lzx.user.mapper.InformMapper;
import com.lzx.user.service.InformService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-12-28
 */
@Service
public class InformServiceImpl extends ServiceImpl<InformMapper, Inform> implements InformService {
    @Resource
    private InformMapper informMapper;

    //保存通知
    @Transactional
    @Override
    public String saveInform(Inform inform) {
        inform.setCreateTime(new Date());
        informMapper.insert(inform);
        return "保存成功";
    }

    //获取通知
    @Override
    public R getInformList(Long userId, int[] type, int current, int size) {
        QueryWrapper<Inform> wrapper = new QueryWrapper<>();
        if (type.length == 1) {
            wrapper.eq("user_id", userId).and(QueryWrapper -> QueryWrapper.eq("type", type[0])).orderByDesc("create_time");
        }
        if (type.length == 2) {
            wrapper.eq("user_id", userId).and(QueryWrapper -> QueryWrapper.eq("type", type[0]))
                    .or().eq("type", type[1]).orderByDesc("create_time");
        }
        if (type.length == 4) {
            wrapper.eq("user_id", userId).and(QueryWrapper ->
                    QueryWrapper
                            .eq("type", type[0])
                            .or().eq("type", type[1])
                            .or().eq("type", type[2])
                            .or().eq("type", type[3])).orderByDesc("create_time");
        }
        Page<Inform> page = new Page<>(current, size);
        informMapper.selectPage(page, wrapper);

        //统计通知未读数量
        wrapper.eq("is_read", 0);
        Long count = informMapper.selectCount(wrapper);

        Map<String, Object> map = new HashMap<>();
        List<Inform> list = page.getRecords();//当前页数据
        long total = page.getTotal();//总记录数
        map.put("list", list);
        map.put("total", total);
        map.put("count", count);
        return R.ok().data("map", map);
    }

    //修改是否已读
    @Override
    public R updateIsRead(Long userId, Long id) {
        QueryWrapper<Inform> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).eq("id", id);
        Inform inform = informMapper.selectOne(wrapper);
        inform.setIsRead(1);
        informMapper.updateById(inform);
        return R.ok().message("修改成功");
    }

    //获取系统通知
    @Override
    public R getSystemInform(int type, int current, int size) {
        QueryWrapper<Inform> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type).orderByDesc("create_time");
        Page<Inform> page = new Page<>(current, size);
        informMapper.selectPage(page, wrapper);
        //统计通知未读数量
        wrapper.eq("is_read", 0);
        Long count = informMapper.selectCount(wrapper);
        Map<String, Object> map = new HashMap<>();
        List<Inform> list = page.getRecords();//当前页数据
        long total = page.getTotal();//总记录数
        map.put("list", list);
        map.put("total", total);
        map.put("count", count);
        return R.ok().data("map", map);
    }

    //修改用户头像(通知)
    @Transactional
    @Override
    public String updateAvatarToInform(OssVO ossVO) {
        QueryWrapper<Inform> wrapper = new QueryWrapper<>();
        wrapper.eq("sender", ossVO.getId());
        List<Inform> informList = informMapper.selectList(wrapper);
        for (Inform inform : informList) {
            inform.setAvatar(ossVO.getAvatar());
            informMapper.updateById(inform);
        }
        return "修改成功";
    }

    //修改用户昵称(通知)
    @Transactional
    @Override
    public String updateNicknameToInform(NicknameVO nicknameVO) {
        QueryWrapper<Inform> wrapper = new QueryWrapper<>();
        wrapper.eq("sender", nicknameVO.getId());
        List<Inform> informList = informMapper.selectList(wrapper);
        for (Inform inform : informList) {
            inform.setNickname(nicknameVO.getNickname());
            informMapper.updateById(inform);
        }
        return "修改成功";
    }
}
